﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Routing;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Brower {
    public partial class NewsController : BaseController {
        private static INews _news;
        private static IClassify _classify;
        private const string _moduleKey = "News";
        private const int _pageSize = 10;

        protected override void Initialize(RequestContext requestContext) {
            base.Initialize(requestContext);
            this.SiteMapPathAdd("首页", Url.Action("Index", "Home"));
        }

        public NewsController() {
            _news = DataBaseExecuter.CreateClassByName<INews>("News");
            _classify = DataBaseExecuter.CreateClassByName<IClassify>("Classify");
        }

        public ActionResult Index() {
            this.SiteMapPathAdd("新闻中心");

            int pageCount;
            return View(new NewsViewModel() {
                NewsList = _news.GetNewsList(0, int.MaxValue, 1, out pageCount),
                ClassifyList = _classify.GetClassifyByModuleKey(_moduleKey)
            });
        }

        public ActionResult View([DefaultValue(0)]int id) {
            NewsEntity ne = _news.GetNewsById(id);

            this.SiteMapPathAdd("新闻中心", Url.Action("Index"));
            this.SiteMapPathAdd(ne.Title);

            int pageCount;
            return View(new NewsViewModel() {
                News = _news.GetNewsById(id),
                NewsList = _news.GetNewsList(ne.ClassifyId, _pageSize, 1, out pageCount),
                ClassifyList = _classify.GetClassifyByModuleKey(_moduleKey)
            });
        }

        [HttpGet]
        public JsonResult Search() {
            string key = Request.QueryString["Key"];
            string pageIndex = Request.QueryString["PageIndex"];
            int classifyId = int.Parse(Request.QueryString["Classify"]);
            string url = string.Format("javascript:AjaxPage('{0}',{1});",
                Url.Action("Search", new {
                    PageIndex = "{0}",
                    Classify = classifyId,
                    Key = key
                }), classifyId);
            return this.NewsJson(url,
                string.IsNullOrEmpty(pageIndex) ? 1 : int.Parse(pageIndex),
                classifyId, key);
        }

        [HttpGet]
        public JsonResult NewsListByClassify([DefaultValue(1)]int pageIndex, [DefaultValue(0)]int classId) {
            string url = string.Format("javascript:AjaxPage('{0}',{1});",
                Url.Action("NewsListByClassify", new {
                    pageIndex = "{0}",
                    classId = classId,
                }), classId);

            return this.NewsJson(url, pageIndex, classId, string.Empty);
        }

        private JsonResult NewsJson(string url, int pageIndex, int classifyId, string key) {
            int pageCount;
            List<NewsEntity> newsList = _news.GetNewsList(
                classifyId, key, _pageSize, pageIndex, out pageCount);
            // 把 content 去掉, 节省传输的时间
            List<NewsEntity> newsListTemp = new List<NewsEntity>();
            if (newsList != null) {
                foreach (NewsEntity ne in newsList) {
                    ne.Content = string.Empty;
                    ne.Title = Methods.SubString(ne.Title, 20);
                    newsListTemp.Add(ne);
                }
            }

            PagerHelper helper = new PagerHelper();
            var o = this.CreatePager(url, pageCount, pageIndex);
            helper.Clone(o);
            string pageHtml = helper.CreateHtml();

            return this.Json(new {
                L = newsListTemp,
                P = pageHtml
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
